/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShortcutKeyDialog.java
 *
 * Created on Jun 16, 2011, 3:51:48 PM
 */
package thesis.view;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
import util.Config;
import thesis.MainFrame;
import util.ShortcutAnnotation;

/**
 *
 * @author anhdung
 */
public class ShortcutKeyDialog extends javax.swing.JDialog {

    /** Creates new form ShortcutKeyDialog
     * @param parent
     * @param modal
     * @param mapShortcut 
     * @param tagPanel  
     */
    public ShortcutKeyDialog(java.awt.Frame parent, boolean modal,
            HashMap<String, String> mapShortcut, TaggingPanel tagPanel) {
        super(parent, modal);
        this.mapConfig = mapShortcut;
        this.tagPanel = tagPanel;
        text = "";
        initComponents();
//        createMap();
    }

    /**
     * Tao map giua cac textfield cua dialog voi cac field co the thay doi shortcut cua TaggingPanel
     * thong qua file properties ShortcutDialog_Tagging_Map
     */
    private void createMap() {
        ResourceBundle rs = ResourceBundle.getBundle("thesis.view.resources.ShortcutDialog_Tagging_Map");
        map = new HashMap<JTextField, Object>();
        valueMap = new HashMap<JTextField, String>();

        for (String tfName : rs.keySet()) {
            String taggingFieldName = rs.getString(tfName);
            Field dialogField = null;
            Field taggingField = null;
            try {
                dialogField = ShortcutKeyDialog.class.getDeclaredField(tfName);
                taggingField = TaggingPanel.class.getDeclaredField(taggingFieldName);
                taggingField.setAccessible(true);
                JTextField textField = (JTextField) dialogField.get(this);
                map.put(textField, taggingField.get(tagPanel));
                valueMap.put(textField, textField.getText());
            } catch (Exception ex) {
                Logger.getLogger(ShortcutKeyDialog.class.getName()).log(Level.SEVERE, null, ex);
            }// end try catch
        }// end foreach tfName
    }// end createMap method

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        perTF = new javax.swing.JTextField();
        locTF = new javax.swing.JTextField();
        orgTF = new javax.swing.JTextField();
        posTF = new javax.swing.JTextField();
        jobTF = new javax.swing.JTextField();
        dateTF = new javax.swing.JTextField();
        applyButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel20 = new javax.swing.JLabel();
        splitTF = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        clickTF = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        untagTF = new javax.swing.JTextField();
        combineTF = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        browseTF = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        modeTF = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        undoTF = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        redoTF = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        saveTF = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        saveAsTF = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Shortcut key");
        setMinimumSize(new java.awt.Dimension(900, 600));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Shortcut Key List");
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 20, -1, -1));

        jLabel2.setText("Person Button:");
        jLabel2.setName("jLabel2"); // NOI18N
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 50, 107, -1));

        jLabel3.setText("Location Button:");
        jLabel3.setName("jLabel3"); // NOI18N
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 100, 107, -1));

        jLabel4.setText("Organization Button:");
        jLabel4.setName("jLabel4"); // NOI18N
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 150, -1, -1));

        jLabel5.setText("Position Button:");
        jLabel5.setName("jLabel5"); // NOI18N
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 200, 107, -1));

        jLabel6.setText("Job Button:");
        jLabel6.setName("jLabel6"); // NOI18N
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 250, 107, -1));

        jLabel7.setText("Date Button:");
        jLabel7.setName("jLabel7"); // NOI18N
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 300, -1, -1));

        ShortcutListener perListener = new ShortcutListener(perTF);
        perTF.setName("perTF"); // NOI18N
        perTF.addKeyListener(perListener);
        perTF.addFocusListener(perListener);
        perTF.setText(mapConfig.get(Config.PER_SHORTCUT));
        getContentPane().add(perTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 170, -1));

        ShortcutListener locListener = new ShortcutListener(locTF);
        locTF.setName("locTF"); // NOI18N
        locTF.addKeyListener(locListener);
        locTF.addFocusListener(locListener);
        locTF.setText(mapConfig.get(Config.LOC_SHORTCUT));
        getContentPane().add(locTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 100, 170, -1));

        ShortcutListener orgListener = new ShortcutListener(orgTF);
        orgTF.setName("orgTF"); // NOI18N
        orgTF.addKeyListener(orgListener);
        orgTF.addFocusListener(orgListener);
        orgTF.setText(mapConfig.get(Config.ORG_SHORTCUT));
        getContentPane().add(orgTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 150, 170, -1));

        ShortcutListener posListener = new ShortcutListener(posTF);
        posTF.setName("posTF"); // NOI18N
        posTF.addKeyListener(posListener);
        posTF.addFocusListener(posListener);
        posTF.setText(mapConfig.get(Config.POS_SHORTCUT));
        getContentPane().add(posTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 200, 170, -1));

        ShortcutListener jobListener = new ShortcutListener(jobTF);
        jobTF.setName("jobTF"); // NOI18N
        jobTF.addKeyListener(jobListener);
        jobTF.addFocusListener(jobListener);
        jobTF.setText(mapConfig.get(Config.JOB_SHORTCUT));
        getContentPane().add(jobTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 250, 170, -1));

        ShortcutListener dateListener = new ShortcutListener(dateTF);
        dateTF.setName("dateTF"); // NOI18N
        dateTF.addKeyListener(dateListener);
        dateTF.addFocusListener(dateListener);
        dateTF.setText(mapConfig.get(Config.DATE_SHORTCUT));
        getContentPane().add(dateTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 300, 170, -1));

        applyButton.setText("Apply");
        applyButton.setName("applyButton"); // NOI18N
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });
        getContentPane().add(applyButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 470, -1, -1));

        cancelButton.setText("Cancel");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 470, -1, -1));

        jLabel20.setText("Normal click Button:");
        jLabel20.setName("jLabel20"); // NOI18N
        getContentPane().add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 50, -1, -1));

        ShortcutListener segListener = new ShortcutListener(splitTF);
        splitTF.setName("splitTF"); // NOI18N
        splitTF.addKeyListener(segListener);
        splitTF.addFocusListener(segListener);
        splitTF.setText(mapConfig.get(Config.SPLIT_SHORTCUT));
        getContentPane().add(splitTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 200, 190, -1));

        jLabel8.setText("Untag Button:");
        jLabel8.setName("jLabel8"); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 100, 90, -1));

        ShortcutListener clickListener = new ShortcutListener(clickTF);
        clickTF.setName("clickTF"); // NOI18N
        clickTF.addKeyListener(clickListener);
        clickTF.addFocusListener(clickListener);
        clickTF.setText(mapConfig.get(Config.CLICK_SHORTCUT));
        getContentPane().add(clickTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 50, 190, -1));

        jLabel12.setText("Combine Button:");
        jLabel12.setName("jLabel12"); // NOI18N
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 150, 110, -1));

        jLabel21.setText("Split Button:");
        jLabel21.setName("jLabel21"); // NOI18N
        getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 200, 90, -1));

        ShortcutListener untagListener = new ShortcutListener(untagTF);
        untagTF.setName("untagTF"); // NOI18N
        untagTF.addKeyListener(untagListener);
        untagTF.addFocusListener(untagListener);
        untagTF.setText(mapConfig.get(Config.UNTAG_SHORTCUT));
        getContentPane().add(untagTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 100, 190, -1));

        ShortcutListener merListener = new ShortcutListener(combineTF);
        combineTF.setName("combineTF"); // NOI18N
        combineTF.addKeyListener(merListener);
        combineTF.addFocusListener(merListener);
        combineTF.setText(mapConfig.get(Config.COMBINE_SHORTCUT));
        getContentPane().add(combineTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 150, 190, -1));

        jLabel22.setText("Browse Button:");
        jLabel22.setName("jLabel22"); // NOI18N
        getContentPane().add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 250, 110, -1));

        ShortcutListener browseListener = new ShortcutListener(browseTF);
        browseTF.setName("browseTF"); // NOI18N
        browseTF.addKeyListener(browseListener);
        browseTF.addFocusListener(browseListener);
        browseTF.setText(mapConfig.get(Config.BROWSE_SHORTCUT));
        getContentPane().add(browseTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 250, 190, -1));

        jLabel9.setText("Toggle Mode:");
        jLabel9.setName("jLabel9"); // NOI18N
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 300, 110, -1));

        ShortcutListener modeListener = new ShortcutListener(modeTF);
        modeTF.setName("modeTF"); // NOI18N
        modeTF.addKeyListener(modeListener);
        modeTF.addFocusListener(modeListener);
        modeTF.setText(mapConfig.get(Config.MODE_SHORTCUT));
        getContentPane().add(modeTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 300, 190, -1));

        jLabel10.setText("Undo Button:");
        jLabel10.setName("jLabel10"); // NOI18N
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 360, -1, -1));

        ShortcutListener undoListener = new ShortcutListener(undoTF);
        undoTF.setName("undoTF"); // NOI18N
        undoTF.addKeyListener(undoListener);
        undoTF.addFocusListener(undoListener);
        undoTF.setText(mapConfig.get(Config.UNDO_SHORTCUT));
        getContentPane().add(undoTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 360, 170, -1));

        jLabel11.setText("Redo Button:");
        jLabel11.setName("jLabel11"); // NOI18N
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 360, -1, -1));

        ShortcutListener redoListener = new ShortcutListener(redoTF);
        redoTF.setName("redoTF"); // NOI18N
        redoTF.addKeyListener(redoListener);
        redoTF.addFocusListener(redoListener);
        redoTF.setText(mapConfig.get(Config.REDO_SHORTCUT));
        getContentPane().add(redoTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 360, 190, -1));

        jLabel13.setText("Save Button:");
        jLabel13.setName("jLabel13"); // NOI18N
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 420, -1, -1));

        ShortcutListener saveListener = new ShortcutListener(saveTF);
        saveTF.setName("saveTF"); // NOI18N
        saveTF.addKeyListener(saveListener);
        saveTF.addFocusListener(saveListener);
        saveTF.setText(mapConfig.get(Config.SAVE_SHORTCUT));
        getContentPane().add(saveTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 420, 170, -1));

        jLabel14.setText("Save As Button:");
        jLabel14.setName("jLabel14"); // NOI18N
        getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 420, -1, -1));

        ShortcutListener saveAsListener = new ShortcutListener(saveAsTF);
        saveAsTF.setName("saveAsTF"); // NOI18N
        saveAsTF.addKeyListener(saveAsListener);
        saveAsTF.addFocusListener(saveAsListener);
        saveAsTF.setText(mapConfig.get(Config.SAVE_AS_SHORTCUT));
        getContentPane().add(saveAsTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 420, 190, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        // TODO add your handling code here:
        Class c = TaggingPanel.class;
        /*
         * Luu cac thay doi vao mapConfig
         */
        mapConfig.put(Config.PER_SHORTCUT, perTF.getText());

        mapConfig.put(Config.LOC_SHORTCUT, locTF.getText());

        mapConfig.put(Config.ORG_SHORTCUT, orgTF.getText());

        mapConfig.put(Config.POS_SHORTCUT, posTF.getText());

        mapConfig.put(Config.JOB_SHORTCUT, jobTF.getText());

        mapConfig.put(Config.DATE_SHORTCUT, dateTF.getText());

        mapConfig.put(Config.UNTAG_SHORTCUT, untagTF.getText());

        mapConfig.put(Config.CLICK_SHORTCUT, clickTF.getText());

        mapConfig.put(Config.COMBINE_SHORTCUT, combineTF.getText());

        mapConfig.put(Config.SPLIT_SHORTCUT, splitTF.getText());

        mapConfig.put(Config.BROWSE_SHORTCUT, browseTF.getText());
        
        mapConfig.put(Config.MODE_SHORTCUT, modeTF.getText());
        
        mapConfig.put(Config.UNDO_SHORTCUT, undoTF.getText());
        
        mapConfig.put(Config.REDO_SHORTCUT, redoTF.getText());
        
        mapConfig.put(Config.SAVE_SHORTCUT, saveTF.getText());
        
        mapConfig.put(Config.SAVE_AS_SHORTCUT, saveAsTF.getText());

        /*
         * Thay doi shortcut cho cac button cua Tagging panel
         */
        try {
            Field[] fields = c.getDeclaredFields();

            Method setInfoButton = c.getDeclaredMethod("setInfo", Object.class);
            setInfoButton.setAccessible(true);
            for (Field field : fields) {
                if (field.getAnnotation(ShortcutAnnotation.class) != null
                        && field.getAnnotation(ShortcutAnnotation.class).type() == 1) {
                    /*
                     * Chi xet cac field co type = 1 la cac field cho phep thay doi shortcut
                     */
                    field.setAccessible(true);
                    setInfoButton.invoke(tagPanel, field.get(tagPanel));
                }// end if field.getAnnotation
            }// end foreach field
            MainFrame.storeConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
        dispose();

    }//GEN-LAST:event_applyButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        applyButton.requestFocus();
    }//GEN-LAST:event_formWindowOpened

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    class ShortcutListener extends KeyAdapter implements FocusListener {

        public ShortcutListener(JTextField textField) {
            this.textField = textField;
        }

        @Override
        /**
         * Override phuong thuc cua KeyAdapter
         */
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            StringBuilder out = new StringBuilder();
            if (e.isControlDown()) {
                out.append("ctrl ");
            }
            if (e.isAltDown()) {
                out.append("alt ");
            }
            if (e.isShiftDown()) {
                out.append("shift ");
            }// end if
            if (!out.toString().equals("")) {
                out.append("+ ");
            }
            String keyPress = KeyEvent.getKeyText(keyCode);
            if (keyPress.equalsIgnoreCase("ctrl") || keyPress.equalsIgnoreCase("alt")
                    || keyPress.equalsIgnoreCase("windows") || keyPress.equalsIgnoreCase("shift")) {
                textField.setText("");
            } else {
                out.append(KeyEvent.getKeyText(keyCode));
                textField.setText(out.toString());
            }
        }

        /**
         * Implements cac phuong thuc cua interface FocusListener
         * @param e 
         */
        public void focusGained(FocusEvent e) {
            text = textField.getText();
            textField.setText("");
        }

        public void focusLost(FocusEvent e) {
            if (textField.getText().equals("")) {
                textField.setText(text);
            }// end if textField.getText().equals("")
        }
        private JTextField textField;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JTextField browseTF;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField clickTF;
    private javax.swing.JTextField combineTF;
    private javax.swing.JTextField dateTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jobTF;
    private javax.swing.JTextField locTF;
    private javax.swing.JTextField modeTF;
    private javax.swing.JTextField orgTF;
    private javax.swing.JTextField perTF;
    private javax.swing.JTextField posTF;
    private javax.swing.JTextField redoTF;
    private javax.swing.JTextField saveAsTF;
    private javax.swing.JTextField saveTF;
    private javax.swing.JTextField splitTF;
    private javax.swing.JTextField undoTF;
    private javax.swing.JTextField untagTF;
    // End of variables declaration//GEN-END:variables
    /*
     * Luu gia tri truoc khi thay doi cua textfield
     */
    private String text;
    private TaggingPanel tagPanel;
    private HashMap<String, String> mapConfig;
    /*
     * Map giua cac JTextField shortcut cua dialog voi cac field shortcut cua TaggingPanel
     */
    HashMap<JTextField, Object> map;
    HashMap<JTextField, String> valueMap;
}
